<?php
include("function.php");
include_once ("./class/ulevel_class.php");
header("Content-Type: text/html;charset=utf-8");
include_once("./class/system_class.php");
//include_once("./alisms/sendSms.php");
$_system=new system_class();
$systemparameters=$_system->system_information(1);

session_start();

unset($_SESSION['ID']);
unset($_SESSION['NickName']);
unset($_SESSION['UserID']);
unset($_SESSION['isboss']);

if(empty($_SESSION['wopenid'])){

    $code = $_GET['code'];
    if(empty($code)){
        //获取当前页面完整链接
        //获取来源地址
        $URL['PHP_SELF'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : (isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['ORIG_PATH_INFO']);   //当前页面名称
        $URL['DOMAIN'] = $_SERVER['SERVER_NAME'];  //域名(主机名)
        $URL['QUERY_STRING'] = $_SERVER['QUERY_STRING'];   //URL 参数
        $URL['URI'] = $URL['PHP_SELF'].($URL['QUERY_STRING'] ? "?".$URL['QUERY_STRING'] : "");
        $nowurl = "http://".$URL['DOMAIN'].$URL['PHP_SELF'].($URL['QUERY_STRING'] ? '?'.$URL['QUERY_STRING'] : ""); //完整URL地址

        $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$systemparameters['w_appid'].'&redirect_uri='.urlencode($nowurl).'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
        header("Location:".$url);
        exit;
    }
    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$systemparameters['w_appid'].'&secret='.$systemparameters['w_appsercret'].'&code='.$code.'&grant_type=authorization_code';
    $data = https_request($url);
    $data = json_decode($data,true);

    if($data['errcode'] != ''){
        echo "<script language=javascript>alert('微信登录暂时有误，请使用账号登录.');top.location='../index.php'</script>";exit;
    }
    $_SESSION['wopenid'] = $data['openid'];

}

$sql = "SELECT * FROM `member` WHERE openid='".$_SESSION['wopenid']."'";

$query = mysql_query($sql);
if (mysql_num_rows($query)>0){
    while($row=mysql_fetch_array($query)){
        $us=getMemberbyNickName($row['userid']);
        if ($us['islock']==0){
            $_SESSION['ID']=$us['id'];
            $_SESSION['nickname']=$us['nickname'];
            $_SESSION['username']=$us['username'];
            $_SESSION['userid']=$us['userid'];
            $_SESSION['isboss']=$us['isboss'];
            //$_SESSION['sclogin']=$us['sclogin'];
            $_SESSION['bdid']=$us['bdid'];
            $_SESSION['isbd']=$us['isbd'];
            $_SESSION['ispay']=$us['ispay'];
            $_SESSION['ulevel']=$us['ulevel'];
            $_SESSION['bdlevel']=$us['bdlevel'];
            $_SESSION['ppath']=$us['ppath'];
            $_SESSION['dzb']=$us['dianzibi'];
            if ($us['id']==1){
                $_SESSION['bdname']=$us['nickname'];
            }else{
                if ($us['isbd']==2){
                    $_SESSION['bdname']=$us['nickname'];
                }else{
                    $_SESSION['bdname']=$us['bdname'];
                }
            }
            $_SESSION['bclogin']=now();
            //$member_update['sclogin']=now();
            //edit_update_cl('member',$member_update,$us['id']);
            if(@$_GET['action_url'] == 'shop'){
                echo "<script language=javascript>window.location.href='./mobile/goodslist3.php'</script>";
            }else{
                echo "<script language=javascript>window.location.href='./mobile/main2.php'</script>";
            }

        }else{
            echo "<script language=javascript>alert('您已被管理员锁定,无法登陆,请联系管理员.');window.location.href='?'</script>";
        }
        exit;
    }
}

// $ulevel = new ulevel_class();
// $iul = $ulevel->getulevelbyulevel (1);
// $nowdate=date('Y-m-d H:i:s',strtotime('+8 hour -'.$iul['yl29'].' hour'));//24小时

// $sql="DELETE member FROM member LEFT JOIN huikuan ON member.id = huikuan.uid  WHERE member.id<>1 and member.pdt <= '".$nowdate."' and huikuan.id  IS NULL ";
// $query = mysql_query($sql);

if ($_POST['loginnow'] == "loginnow"){
    if($_POST['txtValidCode'] == $_SESSION['sms'.$_POST['txtUserAccount']]){
        if(systemstatus()){
            if (checkLogin($_POST['txtUserAccount'],$_POST['txtPassword'])){

                $us=getMemberbyNickName($_POST['txtUserAccount']);
                if ($us['islock']==0){
                    $_SESSION['ID']=$us['id'];
                    $_SESSION['nickname']=$us['nickname'];
                    $_SESSION['username']=$us['username'];
                    $_SESSION['userid']=$us['userid'];
                    $_SESSION['isboss']=$us['isboss'];
                    //$_SESSION['sclogin']=$us['sclogin'];
                    $_SESSION['bdid']=$us['bdid'];
                    $_SESSION['isbd']=$us['isbd'];
                    $_SESSION['ispay']=$us['ispay'];
                    $_SESSION['ulevel']=$us['ulevel'];
                    $_SESSION['bdlevel']=$us['bdlevel'];
                    $_SESSION['ppath']=$us['ppath'];
                    $_SESSION['dzb']=$us['dianzibi'];
                    if ($us['id']==1){
                        $_SESSION['bdname']=$us['nickname'];
                    }else{
                        if ($us['isbd']==2){
                            $_SESSION['bdname']=$us['nickname'];
                        }else{
                            $_SESSION['bdname']=$us['bdname'];
                        }
                    }
                    $_SESSION['bclogin']=now();
                    //$member_update['sclogin']=now();
                    //edit_update_cl('member',$member_update,$us['id']);
                    //绑定微信openid
                    $sqll = "UPDATE member set openid='".$_SESSION['wopenid']."' WHERE id=".$us['id'];
                    $query = mysql_query($sqll);
                    if(@$_GET['action_url'] == 'shop'){
                        echo "<script language=javascript>window.location.href='./mobile/goodslist3.php'</script>";
                    }else{
                        echo "<script language=javascript>window.location.href='./mobile/main2.php'</script>";
                    }
                }else{
                    echo "<script language=javascript>alert('您已被管理员锁定,无法登陆,请联系管理员.');window.location.href='?'</script>";
                }

            }else{
                echo "<script language=javascript>alert('用户名或密码错误.');window.location.href='?'</script>";
            }
        }else{
            echo "<script language=javascript>alert('系统维护,暂时关闭,给您带来不便我们感到万分抱歉.');window.location.href='?'</script>";
        }
    }else{
        //echo $_SESSION['sms'.$_POST['txtUserAccount']];exit;
        echo "<script language=javascript>alert('验证码错误.');window.location.href='?'</script>";
    }
}else{
    $_SESSION['ID']=null;
    $_SESSION['nickname']=null;
    $_SESSION['userid']=null;
    $_SESSION['isboss']=null;
    $_SESSION['pass2']=null;
    $_SESSION['pass3']=null;
    $_SESSION['bdname']=null;
    $_SESSION['bdid']=null;

}
?>
<!DOCTYPE HTML>

<META http-equiv="Content-Type" content="text/html; charset=utf-8"> <!--[if gte IE 7]>
<meta name="msapplication-window" content="width=1024;height=768" />
<![endif]-->
<META http-equiv="X-UA-Compatible" content="requiresActiveX=true">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
<TITLE></TITLE>

<SCRIPT src="js/jquery.min.js" type="text/javascript"></SCRIPT>

<link rel="stylesheet" type="text/css" href="./mobile/css/loaders.min.css"/>
<link rel="stylesheet" type="text/css" href="./mobile/css/loading.css"/>
<link rel="stylesheet" type="text/css" href="./mobile/css/base.css"/>
<link rel="stylesheet" type="text/css" href="./mobile/css/style.css"/>


<style>

    html, body { font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif;}

    ul, ol { padding: 0;}



    .banner { position: relative; overflow: auto; text-align: center;}

    .banner li { list-style: none; }

    .banner ul li { float: left; }

</style>
<script>

    $(document).ready(function(e) {

        $('#b02').unslider();

    });

</script>
<SCRIPT type="text/javascript">
    $(function(){
        var $username = $("#login_username");
        var $password = $("#login_password");
        var $message = $("#login_err_msg");
        var $buttons = $("#btn_login");
        $username.focus(function(){
            $(this).siblings("label").text("");
        }).blur(function(){
            if($username.val() == ""){
                $(this).siblings("label").text("账 号");
            }
        });
        $password.focus(function(){
            $(this).siblings("label").text("");
        }).blur(function(){
            if($password.val() == ""){
                $(this).siblings("label").text("登录密码");
            }
        });

        //登录按钮
        $buttons.click(function(){
            doSubmit();
        });

        //维护弹窗
        $( "#dialog" ).dialog({
            autoOpen: false,
            modal: true,
            width: 400,
            buttons: [
                {
                    text: "确定",
                    click: function() {
                        $( this ).dialog( "close" );
                    }
                }
            ]
        });

    });
    function doSubmit(){
        var name = $("#login_username").val();
        var pwd = $("#login_password").val();
        var valid = true;
        if(name == ""){
            valid = false;
            $("#login_err_msg").text("请填写账号!");
            $("#login_err_msg").show();
        }
        if(pwd == ""){
            valid = false;
            $("#login_err_msg").text("请填写登录密码!");
            $("#login_err_msg").show();
        }
        if(valid){
            $("#login_err_msg").text("");
            $("#login_err_msg").hide();
            $("#first_login").submit();
        }
    }
    function keyEvent(evt){
        evt = (evt) ? evt : ((window.event) ? window.event : "");
        var keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.charCode);
        if (keyCode == 13) {
            doSubmit();
        }
    }
</SCRIPT>

<SCRIPT>
    // 保持自己是最TOP的
    if (top.location != self.location)
        top.location=self.location;
</SCRIPT>

<SCRIPT language="javascript">
    function validate() {

        var oForm = document.loginForm;
        var numbersOnly = /^[0-9]+$/;
        if (oForm.txtUserAccount.value == "") {
            alert("Please enter the login account！！！");
            oForm.txtUserAccount.focus();
            oForm.txtUserAccount.select();
        }else if (oForm.txtPassword.value == "") {
            alert("Please enter the login password！！！");
            oForm.txtPassword.focus();
            oForm.txtPassword.select();
        }else {
            //oForm.btnSubmit.disabled = true;
            return true;
        }
        return false;
    }
    /*else if (oForm.txtValidCode.value == "") {
     alert("Please enter the verification code！！！");
     oForm.txtValidCode.focus();
     oForm.txtValidCode.select();
     }*/
    window.onload = function () {
        try {
            if(document.loginForm.txtUserAccount.value == '') {
                document.loginForm.txtUserAccount.focus();
            }else {
                document.loginForm.txtPassword.focus();
            }
        }catch(e) {}
    }//
//    function getCode()
//    {
//        document.writeln("<img style=\"cursor:pointer;cursor:hand;\" title=\"看不清？点击更换图片。\" src=\"code.php"+"\"  onclick=\"chgSrc(this);\"/>");
//    }
    function chgSrc(obj)
    {
        if(obj.classList.contains('yifa')){

            return false;
        }
        var tell = $("input[name=txtUserAccount]").val();
        if(tell == ''){
            alert('请输入账号');
            return false;
        }

        obj.className = 'yifa';
        obj.style.cssText="display: inline-block;font-size:1em;background:#ccc;border-radius:3px;width:150px;text-align: center;height:78px;line-height: 78px;color:red;";
        obj.innerHTML='发送中...';

        $.ajax({
            type: "GET",
            url: "ajaxsms.php?tel="+tell,
            dataType:'json',

            success:function(daa){

                if(daa.code == 2){
                    alert(daa.msg);
                    obj.className = '';
                    obj.style.cssText="display: inline-block;font-size:1em;background:#ccc;border-radius:3px;width:150px;text-align: center;height:78px;line-height: 78px;color:#000;";
                    obj.innerHTML='获取验证码';
                }else{
                    obj.innerHTML='已发送';
                }
            }
        });

    }
</SCRIPT>

<META name="GENERATOR" content="MSHTML 11.00.9600.17041"></HEAD>
<BODY style="margin: 0px; background-color: rgb(255, 255, 255);">
    <?php include_once ("./mobile/public/loading.html");?>
    <header class="top-header page-header">
        <a class="text texta" href="javascript:history.back(-1);">返回</a>
        <h3>绑定账号</h3>
        <a class="text texta" href=""></a>
    </header>
    <div class="login">
        <div>
            <img src="unslider/11.png" width="100%" height="30%"><!--    <img src="mobile/images/mobile_login.jpg" width="100%" height="30%">-->
        </div>
        <form name="loginForm" onsubmit="return validate();" action="mobileBang.php" method="POST">
            <INPUT type=hidden value=loginnow name=loginnow>
            <ul>
                <li>
                    <img src="./mobile/images/login.png"/>
                    <label>账号</label>
                    <input class="input-text" name="txtUserAccount" type="text" placeholder="请输入账号" id="login_username" onfocus="if(this.value==' Please enter your user name')this.value='';"/>
                </li>
                <li>
                    <img src="./mobile/images/password.png"/>
                    <label>密码</label>
                    <input class="input-text" name="txtPassword" type="password" placeholder="请输入密码" id="login_password" onfocus="if(this.value==' Please enter your password')this.value='';"/>
                </li>
                <li>
                    <img src="./mobile/images/yz.png"/>
                    <label>验证码</label>
                    <input style="width: 13rem" class="input-text" type="text" name="txtValidCode" onfocus="if(this.value==' Please Input Valid Code!')this.value='';" placeholder="填写验证码"/>
<!--                    <a onclick="chgSrc(this);" style="display: inline-block;font-size:1em;background:#ccc;border-radius:3px;width:150px;text-align: center;height:78px;line-height: 78px;color:#000;">获取验证码</a>-->
                    <a onclick="chgSrc(this);" style="display: inline-block;font-size:1em;background:#ccc;border-radius:3px;width:70px;text-align: center;height:30px;line-height: 30px;color:#000;">获取验证码</a>
                </li>
            </ul>
            <input type="submit" value="登录" id="btn_login" onkeydown="keyEvent(event);"/>
<!--            <div><a href="" style="color:#ff4e00;font-size: 1.3rem;margin-left: 30rem;">忘记密码?</a></div>-->
        </form>
    </div>

</BODY></HTML>